﻿using Autofac.Core;
using DFGK.WMS.Common.Result;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Net.Mime;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PDAPandianTInventoryQueryController : ControllerBase
    {
        private readonly ITInventoryQuery tTInventoryQuery;
        private readonly ITStocktakingOrderRepository TStocktakingOrderRepository;
        private readonly ITCellRepository tCellRepository1;
        private readonly ITStocktakeResultRecordRepository TStocktakeResultRecordRepository;
        public PDAPandianTInventoryQueryController(ITInventoryQuery tTInventoryQuery, ITStocktakingOrderRepository tStocktakingOrderRepository, ITCellRepository tCellRepository, ITStocktakeResultRecordRepository tStocktakeResultRecordRepository)
        {
            this.tTInventoryQuery = tTInventoryQuery;
            this.TStocktakingOrderRepository = tStocktakingOrderRepository;
            this.tCellRepository1 = tCellRepository;
            this.TStocktakeResultRecordRepository = tStocktakeResultRecordRepository;
        }
        /// <summary>
        /// pda显示盘点单及盘点计划
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPDAlistyi(int pageindex = 1, int pagesize = 10)
        {

            int totalt;
            var list = TStocktakingOrderRepository.GetPDAlistyi(out totalt, pageindex, pagesize);

            ApiItemsResult baseResult = new ApiItemsResult();
            if (list != null)
            {
                baseResult.code = "200";
                baseResult.count = totalt;
                baseResult.message = "已查询";
                baseResult.data = list;
                return Ok(baseResult);
            }
            baseResult.code = "500";
            baseResult.count = totalt;
            baseResult.message = "查询失败";
            return Ok(baseResult);
        }
        [HttpPost]
        public IActionResult GetTInventorywuliao(PandianDanhaodto dto)
        {
            bool pandian = tTInventoryQuery.GetTInventorywuliao(dto.Items, dto.planId, dto.planNumber);
            ApiBaseResult baseResult = new ApiBaseResult();
            if (!pandian)
            {
                baseResult.Code = "500";
                baseResult.Success = false;
                baseResult.Message = "数据错误";
                return Ok(baseResult);
            }
            baseResult.Code = "200";
            baseResult.Success = true;
            baseResult.Message = "";
            return new JsonResult(baseResult);
        }
        /// <summary>
        /// 盘点结果记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Tianjiapandianjilu(TStocktakeResultRecorddtofengzhuang dto)
        {
            string message;
            ApiItemsResult apiItemsResult = new ApiItemsResult();
            bool fanhuishuju = TStocktakeResultRecordRepository.Tianjiapandianjilu(dto,out message);
        
            if (!fanhuishuju)
            {
                apiItemsResult.code = "500";
                apiItemsResult.message = message;
                apiItemsResult.data = fanhuishuju;
                return Ok(apiItemsResult);
            } 
            apiItemsResult.code = "200";
            apiItemsResult.message = message;
            apiItemsResult.data = fanhuishuju;
            return Ok(apiItemsResult);
        }
        /// <summary>
        /// pda修改盘点计划已完成盘点
        /// </summary>
        /// <param name="ordernum"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult PdaXiugaizhuangtai(string ordernum)
        {
            string message;
            ApiItemsResult itemsresult = new ApiItemsResult();
            bool pandian = TStocktakingOrderRepository.UpdateStatetai(ordernum, out message);
            if (pandian)
            {
                itemsresult.code = "200";
                itemsresult.message = "已修改";
                itemsresult.data = pandian; ;
                return Ok(itemsresult);
            }
            itemsresult.code = "500";
            itemsresult.message = "修改失败";
            itemsresult.data = pandian; ;
            return Ok(itemsresult);
        }
      /*  [HttpGet("ExportToExcel")]
        public FileContentResult ExportToExcel(DateTime? startTime, DateTime? endTime, int? billType, int? houseid, string? name)
        {
            var list = _service.GetOuHouseToExcel(startTime, endTime, billType, houseid, name, 1, int.MaxValue, out var totalCount);
            var dic = new Dictionary<string, string>()
            {
                {"OrdeSn","单号" },
                {"MaterialName","物料描述" },
                {"MaterialCode","物料代码" },
                {"isChuku", "状态"},
                {"BatchNo","批次" },
                {"SmallBatchNo","详细批次" },
                {"CellName","仓位名称" },
                {"OutQty","出库数量" },
                {"InvCount","库存剩余数量" },
                {"keChuCount","可出库数量" },
                {"PalletBarcode","托盘条码" },
                {"ProductionTime","生产时间" },
                {"CarCode","车牌号" }
            };
            var exList = list.Select(s => new {
                s.OrdeSn,
                s.MaterialName,
                s.MaterialCode,
                isChuku = s.IsPicking.GetValueOrDefault() ? "已出库" : "未出库",
                s.BatchNo,
                s.SmallBatchNo,
                s.CellName,
                s.CarCode,
                InvCount = (int)s.InvCount.GetValueOrDefault(),
                OutQty = (int)s.OutQty.GetValueOrDefault(),
                keChuCount = (int)s.keChuCount.GetValueOrDefault(),
                s.PalletBarcode,
                s.ProductionTime
            }).ToList();
            var fileBytes = Utils.ExportToExcel(exList, dic);
            return File(fileBytes, MediaTypeNames.Application.Octet, "出库管理.xlsx");*/

        
    }
}
